import java.util.Scanner;

/**
 * Created by IntelliJ IDEA.
 * User: zm
 * Date: 2023/9/16
 */
public class Demo12 {
    public static int N = 100001;
    static int[] h = new int[N];
    static int size = 0;
    public static void down(int u){
        int t = u;
        if(2 * u <= size && h[2 * u] < h[t])t = 2 * u;
        if(2 * u  + 1<= size && h[2 * u + 1] < h[t])t = 2 * u + 1;
        if(t != u){
            int temp = h[t];
            h[t] = h[u];
            h[u] = temp;
            down(t);
        }
    }
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int m = scan.nextInt();
        for(int i = 1; i <= n; i++) h[i] = scan.nextInt();
        size = n;
        for(int i = n / 2; i >= 0; --i)down(i);

        while(m -- > 0){
            System.out.print(h[1] + " ");
            h[1] = h[size];
            size--;
            down(1);
        }
    }
}
